<template>
  <a-modal
    title="编辑指标汇总数据"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="省"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback>
          <a-input placeholder="请输入省" v-decorator="['provName', {rules: [{required: true, message: '请输入省！'}]}]" />
        </a-form-item>
        <a-form-item
          label="城市"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback>
          <a-input placeholder="请输入城市" v-decorator="['cityName']" />
        </a-form-item>
        <a-form-item
          label="地区"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback>
          <a-input placeholder="请输入地区" v-decorator="['countryName']" />
        </a-form-item>
        <a-form-item
          label="类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback>
          <a-input placeholder="请输入类型" v-decorator="['typeId', {rules: [{required: true, message: '请输入类型！'}]}]" />
        </a-form-item>
        <a-form-item
          label="名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback>
          <a-input placeholder="请输入指标名称" v-decorator="['name', {rules: [{required: true, message: '请输入指标名称！'}]}]" />
        </a-form-item>
        <a-form-item
          label="值"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback>
          <a-input placeholder="请输入指标值" v-decorator="['value', {rules: [{required: true, message: '请输入指标值！'}]}]" />
        </a-form-item>
        <a-form-item
          label="时间级别"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback>
          <a-input placeholder="请输入时间级别" v-decorator="['timeLevel', {rules: [{required: true, message: '请输入时间级别！'}]}]" />
        </a-form-item>
        <a-form-item
          label="年份"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback>
          <a-input placeholder="请输入" v-decorator="['year']" />
        </a-form-item>
        <a-form-item
          label="月份"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback>
          <a-input placeholder="请输入" v-decorator="['month']" />
        </a-form-item>
        <a-form-item
          label="日期"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入" v-decorator="['day']" />
        </a-form-item>
        <a-form-item
          label="季度"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入" v-decorator="['season']" />
        </a-form-item>
        <a-form-item
          label="周"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入" v-decorator="['week']" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import { biIndexDataSumEdit } from '@/api/modular/main/biindexdatasum/biIndexDataSumManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        createDateDateString: '',
        modifyDateDateString: '',
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (record) {
        this.visible = true
        console.log(record)
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              provName: record.provName,
              cityName: record.cityName,
              countryName: record.countryName,
              typeId: record.typeId,
              name: record.name,
              value: record.value,
              timeLevel: record.timeLevel,
              year: record.year,
              month: record.month,
              day: record.day,
              season: record.season,
              week: record.week
            }
          )
        }, 100)
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            biIndexDataSumEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败')//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      onChangecreateDate(date, dateString) {
        this.createDateDateString = dateString
      },
      onChangemodifyDate(date, dateString) {
        this.modifyDateDateString = dateString
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
